Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

226
Vistas
What is the difference between "?" and "= null" in PHP function parameters

I was doing some research but wasn't able to find an answer (probably beacause I did not searched it right)

Consider this piece of code:

public function foo(?array $optionalParam);

And then this one:

public function foo(array $optionalParam = null);

What differs between them? Using PHPstorm I noticed that when I use the ?, it creates a PHPdoc and mark the variable type as type|null. But when I call the function without that argument, PHP screams on my face "you kidding me? where is $optionalParam". In the other side, I managed to use with no problems the =null option.

Sorry if this question is too simple, but i did not find any answers online.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

First of all, the ? goes before the type, not after... other than this:

Using

public function foo(?array $optionalParam);

you are forced to pass something, that can be either null or an array, infact:

<?php
function foo(?array $optionalParam){
  echo "test";
}

foo(); // doesn't work
foo(null); // works
foo([]); // works

where instead using

public function foo(array $optionalParam = null);

will accept null, an array, or 0 parameters

<?php
function foo(array $optionalParam = null){
  echo "test";
}

foo(null); // works
foo(); // work
foo([]); // works
over 4 years ago · Santiago Trujillo Denunciar

0

It's a PHP 7.1 feature called Nullable Types

Both of the lines you wrote are identical.

array ?$optionalParam : either an array or null

array $optionalParam = null : either an array or null

Tho using ? you'd still need to add the parameter when calling the function.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda